package com.lyn.module_custom_network;

import android.util.Log;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;

/**
 * created by lyn at 2021/10/14
 */
public class LynWebSocketClient extends WebSocketClient {

    private static final String TAG = "LynWebSocketClient";

    private LynWebSocketClientCallBack callBack;

    public void setCallBack(LynWebSocketClientCallBack callBack) {
        this.callBack = callBack;
    }

    public LynWebSocketClient(URI serverUri) {
        super(serverUri, new Draft_6455());
    }

    @Override
    public void onOpen(ServerHandshake handShakeData) {
        Log.i(TAG, "onOpen");
    }

    @Override
    public void onMessage(String message) {
        Log.i(TAG, "onMessage:"+message);
        if (callBack==null)return;
        callBack.onMessage(message);
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        Log.i(TAG, "onClose  code:" + code + "reason:" + reason + "remote:" + remote);
    }

    @Override
    public void onError(Exception ex) {
        Log.i(TAG, "onError:"+ex.getMessage());
    }


    public interface LynWebSocketClientCallBack{

        void onMessage(String message);

    }


}
